var fs = require("fs");
var constant = require("../constant/constant");


var i18nLoader = {};
// global var
var i18nMap = new Map();

/************************************************************************************************
 * load each language resource
 * init at www
 ************************************************************************************************/
i18nLoader.load = function() {

    i18nMap.set("i18n_cn_simple",'');
    i18nMap.set("i18n_cn_traditional",'');
    i18nMap.set("i18n_en",'');

    for(var key of i18nMap.keys()) {
        console.log(key);
        var data = fs.readFileSync("../i18n/" + key + ".json");
        i18nMap.set(key,(JSON.parse(data)));
    }
    console.log("i18n init success...");
};

/************************************************************************************************
 * get language resource, if lang don't exists, default lang is english
 * @param   langName
 * @param   fieldName
 ************************************************************************************************/
i18nLoader.get = function (langName,fieldName) {
    if(langName == null || langName == undefined) {
        langName = constant.LANG_EN;
    }
    console.log(i18nMap.get(langName));
    // 对于json, 可以直接.fieldName出来,也可以[fieldName]出来
    return i18nMap.get(langName)[fieldName];
};




module.exports = i18nLoader;